package com.sali.config;

import com.sali.entity.MultipartFileProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.MultipartConfigFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.MultipartConfigElement;

@Configuration
@EnableConfigurationProperties(MultipartFileProperties.class)
public class MultipartFileConfiguration {

    @Autowired
    private MultipartFileProperties multipartFileProperties;


    @Bean
    public MultipartConfigElement multipartConfigElement() {
        MultipartConfigFactory factory = new MultipartConfigFactory();
        // 设置上传文件的临时目录
        factory.setLocation(multipartFileProperties.getLocation());

        factory.setFileSizeThreshold(multipartFileProperties.getFileSizeThreshold());
        factory.setMaxFileSize(multipartFileProperties.getMaxFileSize());
        factory.setMaxRequestSize(multipartFileProperties.getMaxRequestSize());

        return factory.createMultipartConfig();
    }

}
